package com.pub;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import org.springframework.web.filter.OncePerRequestFilter;

//import com.mvc.entity.User;

/**
 * 登录过滤
 * 
 */
public class UserSessionFilter extends OncePerRequestFilter {
	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		/*
		 * 
		 * // 不拦截的url String[] notFilter = new String[]
		 * {"/Home/Index.do","/User/TreeList.do"
		 * ,"/Reg/Index.do","/Reg/Check.do",
		 * "/Reg/Save.do","/Home/Exit.do","/Home/LoginCheck.do"};
		 * 
		 * // 请求的url String url = request.getRequestURI();
		 * 
		 * if(url.indexOf("UniqueduHome")== -1){
		 * 
		 * boolean doFilter = chek(notFilter,url); //
		 * System.out.println("++++++++++++++++++++++++++++++"
		 * +url+"+++++++++++++++++++++++++++++++++++++++"); if(doFilter){ Object
		 * obj = request.getSession().getAttribute("SessionUser");
		 * if(null==obj){ // 如果session中不存在登录者实体，则弹出框提示重新登录 PrintWriter out =
		 * response.getWriter(); String loginPage =
		 * request.getContextPath()+"/Home/Index.do"; StringBuilder builder =
		 * new StringBuilder();
		 * builder.append("<script type=\"text/javascript\">");
		 * builder.append("window.top.location.href='");
		 * builder.append(loginPage); builder.append("';");
		 * builder.append("</script>"); out.print(builder.toString()); }else {
		 * // User user=(User)obj; // String str=user.getRoleName(); String
		 * str=""; //
		 * System.out.println("++++++++++++++++++++++++++=================="
		 * +str); if(!str.equals("超级管理员")){ List
		 * list=(List)request.getSession().getAttribute("SessionList");
		 * if(urlCheck(url,list)){ filterChain.doFilter(request, response);
		 * }else { // 如果session中不存在登录者实体，则弹出框提示重新登录 PrintWriter out =
		 * response.getWriter(); String loginPage =
		 * request.getContextPath()+"/Login/Index.Action"; StringBuilder builder
		 * = new StringBuilder();
		 * builder.append("<script type=\"text/javascript\">");
		 * builder.append("window.top.location.href='");
		 * builder.append(loginPage); builder.append("';");
		 * builder.append("</script>"); out.print(builder.toString()); } }else {
		 * filterChain.doFilter(request, response); } } }else{
		 * filterChain.doFilter(request, response); } }else{
		 * filterChain.doFilter(request, response); }
		 */

		Object obj = request.getSession().getAttribute("SessionMem");
		if (null == obj) {
			// 如果session中不存在登录者实体，则弹出框提示重新登录
			PrintWriter out = response.getWriter();
			String loginPage = request.getContextPath() + "/Login.Action";
			StringBuilder builder = new StringBuilder();
			builder.append("<script type=\"text/javascript\">");
			builder.append("window.top.location.href='");
			builder.append(loginPage);
			builder.append("';");
			builder.append("</script>");
			out.print(builder.toString());

		} else {

			filterChain.doFilter(request, response);
	
	 	}
	 
	}

	/**
	 * @param notFilter
	 *            不拦截的url
	 * @param url
	 *            ：请求的url
	 * @return false：不拦截 true：拦截
	 */
	public boolean chek(String[] notFilter, String url) {
		for (String s : notFilter) {
			if (url.indexOf(s) != -1) {
				return false;
			}

		}
		return true;
	}

	public boolean urlCheck(String url, List list) {
		if (null != list) {
			if (list.contains(url)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}